﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace PanasonicWarranty.UI
{
    public partial class UserManagement : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!AllowCreateUser() && !AllowEditUser() && !AllowDeleteUser())
            {
                RedirectToPage(AccessDeniedURL);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindDataGrid();
                if (!AllowCreateUser())
                    lbtnAddUser.Visible = false;
            }
        }

        private void BindDataGrid()
        {
            if (CurUser.Name.Equals(Constants.AdminRootAccount, StringComparison.CurrentCultureIgnoreCase))
            {
                rptObject.DataSource = DB.pepsi_Users.Select(p => new
                {
                    p.UserID,
                    UserName = p.Name,
                    StaffName = p.pepsi_Staff.StaffName,
                    UnitName = p.pepsi_Unit.Name,
                });
            }
            else
            {
                rptObject.DataSource = DB.pepsi_Users.Where(p => p.Name != Constants.AdminRootAccount).Select(p => new
                {
                    p.UserID,
                    UserName = p.Name,
                    StaffName = p.pepsi_Staff.StaffName,
                    UnitName = p.pepsi_Unit.Name,
                });
            }
            rptObject.DataBind();
        }

        protected void rptObject_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Edit":
                    if (!AllowEditUser())
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"You don't have permission\");", true);
                        return;
                    }
                    ReturnURL = Request.RawUrl;
                    RedirectToPage(ConfigurationManager.AppSettings["EditUserURL"] + "?action=edit&UserID=" + e.CommandArgument);
                    break;
                case "Delete":
                    if (!AllowDeleteUser())
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"You don't have permission\");", true);
                        return;
                    }
                    var deleteObj = DB.pepsi_Users.FirstOrDefault(p => p.UserID == Convert.ToInt32(e.CommandArgument));
                    if (deleteObj != null)
                    {
                        if (deleteObj.Name == Constants.AdminRootAccount)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"You don't have permission\");", true);
                            return;
                        }
                        DB.pepsi_Users.DeleteOnSubmit(deleteObj);
                        DB.SubmitChanges();
                        BindDataGrid();
                    }
                    break;
            }
        }

        protected void lbtnAddUser_Click(object sender, EventArgs e)
        {
            if (!AllowCreateUser())
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"You don't have permission\");", true);
                return;
            }
            ReturnURL = Request.RawUrl;
            RedirectToPage(ConfigurationManager.AppSettings["EditUserURL"] + "?action=add");
        }
    }
}